package com.shenfa.service.work;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.CheckRuleData;
import com.shenfa.bean.Epiboly;
import com.shenfa.bean.Paper;
import com.shenfa.bean.Template;
import com.shenfa.bean.work.BillEList;
import com.shenfa.bean.work.BillPList;
import com.shenfa.bean.work.Check;
import com.shenfa.bean.work.ComposeLog;
import com.shenfa.bean.work.Watch;
import com.shenfa.bean.work.WatchState;
import com.shenfa.bean.work.XmlLog;
import com.shenfa.bean.xml.PointInfo;
import com.shenfa.bean.xml.UserBase;
import com.shenfa.dao.EpibolyDao;
import com.shenfa.dao.PaperDao;
import com.shenfa.dao.ProductDao;
import com.shenfa.dao.systemManager.person.PersonDao;
import com.shenfa.dao.work.XmlLogDao;
import com.shenfa.dao.xml.MakeXmlDao;

public class XmlLogService {

	// 日志
	private static final Logger log = Logger.getLogger(XmlLogService.class);

	@Autowired
	private XmlLogDao xmlLogDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private EpibolyDao epibolyDao;
	@Autowired
	private PaperDao paperDao;
	@Autowired
	private MakeXmlDao makeXmlDao;
	@Autowired
	private PersonDao personDao;

	private List<String> epibolyname;
	private List<String> loginName;
	private List<String> cardName;
	private List<Paper> pp;

	/**
	 * 根据条件查询 XML生成日志
	 * 
	 * @param xmllog
	 * @param skipResults
	 * @param maxResults
	 * @return
	 */
	public List<XmlLog> doGridList(XmlLog xmllog, int skipResults,
			int maxResults) {
		List<XmlLog> list = xmlLogDao.xmlLogGridList(xmllog, skipResults,
				maxResults);
		List<XmlLog> list1 = new ArrayList<XmlLog>();
		if (null == list)
			return null;
		for (int i = 0; i < list.size(); i++) {
			XmlLog xl = list.get(i);
			// 如果关联外包商，则查询对应外包商
			if (null != xl.getBusinpnt_no()) {
				Epiboly t = new Epiboly();
				t.setId(xl.getBusinpnt_no());
				List<Epiboly> l1 = epibolyDao.getList(t, 0, 10);
				if (null != l1 && l1.size() != 0) {
					xl.setBusinpnt_name(l1.get(0).getName());
				}
			}
			// 如果关联预印纸，则查询对应预印纸
			if (null != xl.getPaper_no()) {
				Paper t = new Paper();
				t.setPid(xl.getPaper_no());
				List<Paper> l2 = paperDao.queryPaperByPaperid(t);
				if (null != l2 && l2.size() != 0) {
					xl.setPaper_name(l2.get(0).getPaper_name());
				}
			}
			list1.add(xl);
		}
		return list1;
	}

	/**
	 * 根据账期和登录人ID查询XmlLog
	 * 
	 * @param paramMap
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<Map<String, String>> queryXmlLog(Map<String, String> paramMap,
			int start, int limit) {

		List<Map<String, String>> list = makeXmlDao.queryXmlLog(paramMap,
				start, limit);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				if (null != list.get(i).get("epibolyno")) {
					epibolyname = epibolyDao.getEpibolyName(list.get(i).get(
							"epibolyno"));
					list.get(i).put("businpntName", epibolyname.get(0));
				}
				if (null != list.get(i).get("paperno")) {
					pp = paperDao.queryPaper(list.get(i).get("paperno"));
					list.get(i).put("paperName", pp.get(0).getPaper_name());
				}
				if (null != list.get(i).get("cardid")
						|| "" != list.get(i).get("cardid").trim()) {
					cardName = productDao.queryName(list.get(i).get("cardid"));
					list.get(i).put("cardName", cardName.get(0));
				}
				if (null != list.get(i).get("personno")) {
					loginName = personDao.getLoginNameBypersonno(list.get(i)
							.get("personno"));
					list.get(i).put("loginName", loginName.get(0));
				}
			}
		}
		return list;
	}

	/**
	 * 根据条件查询 电子/纸质合成日志
	 * 
	 * @param Comlog
	 * @param skipResults
	 * @param maxResults
	 * @return
	 */
	public List<ComposeLog> docomposeLogList(ComposeLog Comlog,
			int skipResults, int maxResults) {
		List<ComposeLog> list = new ArrayList<ComposeLog>();
		list = xmlLogDao.composeLogGridList(Comlog, skipResults, maxResults);
		for (int i = 0; i < list.size(); i++) {
			if (null != list.get(i).getBusinpnt_no()) {
				List<String> epnames = epibolyDao.getEpibolyName(list.get(i)
						.getBusinpnt_no());
				for (int j = 0; j < epnames.size(); j++) {
					if (null != epnames.get(j) || epnames.get(j) != "") {
						list.get(i).setBusinpnt_name(epnames.get(j));
					}
				}
			}
			List<String> proNames = productDao.queryName(list.get(i)
					.getCard_id());
			for (int j = 0; j < proNames.size(); j++) {
				if (null != proNames.get(j) || proNames.get(j) != "") {
					list.get(i).setCard_name(proNames.get(j));
				}
			}
			List<Paper> papers = paperDao.queryPaper(list.get(i).getPaper_no());
			for (int k = 0; k < papers.size(); k++) {
				if (null != papers.get(k)
						|| papers.get(k).getPaper_name() != "") {
					list.get(i).setPaper_name(papers.get(k).getPaper_name());
				}
			}
		}
		return list;
	}

	/**
	 * 查询抽样所需的电子/纸质合成日志
	 * 
	 * @param skipResults
	 * @param maxResults
	 * @return
	 */
	public List<ComposeLog> doSampleComposeLog(ComposeLog Comlog,
			int skipResults, int maxResults) {
		List<ComposeLog> list = new ArrayList<ComposeLog>();
		list = xmlLogDao.sampleComposeLogGrid(Comlog, skipResults, maxResults);
		for (int i = 0; i < list.size(); i++) {
			if (null != list.get(i).getBusinpnt_no()) {
				List<String> epnames = epibolyDao.getEpibolyName(list.get(i)
						.getBusinpnt_no());
				for (int j = 0; j < epnames.size(); j++) {
					if (null != epnames.get(j) || epnames.get(j) != "") {
						list.get(i).setBusinpnt_name(epnames.get(j));
					}
				}
			}
			List<String> proNames = productDao.queryName(list.get(i)
					.getCard_id());
			for (int j = 0; j < proNames.size(); j++) {
				if (null != proNames.get(j) || proNames.get(j) != "") {
					list.get(i).setCard_name(proNames.get(j));
				}
			}
			List<Paper> papers = paperDao.queryPaper(list.get(i).getPaper_no());
			for (int k = 0; k < papers.size(); k++) {
				if (null != papers.get(k)
						|| papers.get(k).getPaper_name() != "") {
					list.get(i).setPaper_name(papers.get(k).getPaper_name());
				}
			}
		}
		return list;
	}

	/**
	 * 查询合成监控状态表
	 * 
	 * @return
	 */
	public List<WatchState> watchStateGridList(WatchState watchState,
			int start, int limit) {
		List<WatchState> list = new ArrayList<WatchState>();
		list = xmlLogDao.watchStateGridList(watchState, start, limit);
		return list;
	}

	/**
	 * 查询合成监控状态表总条数
	 * 
	 * @return
	 */
	public Object doWatchlenTotal(WatchState watchState) {
		return xmlLogDao.totalWatchlen(watchState);
	}

	/**
	 * 分发账单(修改账单状态)
	 * 
	 * @param listlog
	 */
	public int upd(String listlog) {
		String[] str = listlog.split(",");
		ComposeLog cl = null;
		for (int i = 0; i < str.length; i++) {
			String[] s = str[i].split("\\|");
			cl = new ComposeLog();
			cl.setPeriod(s[0].trim());
			cl.setCompose_type(s[1]);
			cl.setFilename(s[2]);
			cl.setState(String.valueOf(3));
			int type = xmlLogDao.updComposeLogState(cl);
			if (type == 0) {
				return 0;
			}
		}
		return 1;
	}

	/**
	 * 根据条件查询纸质账单明细信息
	 * 
	 * @param bp
	 * @return
	 */
	public List<BillPList> queryBillp(BillPList bp, int skipResults,
			int maxResults) {
		// String str = "";
		// str = bp.getPeriod().substring(0, 4)+bp.getPeriod().substring(5,
		// 7)+bp.getPeriod().substring(8, 10);
		// bp.setPeriod(str);
		return xmlLogDao.checkBillp(bp, skipResults, maxResults);
	}

	public Object queryBillpTotal(BillPList bp) {
		return xmlLogDao.totalCheckBillp(bp);
	}

	/**
	 * 根据条件查询电子账单明细信息
	 * 
	 * @param be
	 * @return
	 */
	public List<BillEList> queryBille(BillEList b_e, int skipResults,
			int maxResults) {
		// String str = "";
		// str = b_e.getPeriod().substring(0, 4)+b_e.getPeriod().substring(5,
		// 7)+b_e.getPeriod().substring(8, 10);
		// b_e.setPeriod(str);
		return xmlLogDao.checkBille(b_e, skipResults, maxResults);
	}

	/**
	 * watch查询
	 * 
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<Watch> queryWatch(int start, int limit) {
		return xmlLogDao.queryWatch(start, limit);
	}

	public Object queryBilleTotal(BillEList b_e) {
		return xmlLogDao.totalCheckBille(b_e);
	}

	public Object doPagTotal(XmlLog xmllog) {
		return xmlLogDao.totalListlen(xmllog);
	}

	public Object doComPagTotal(ComposeLog Comlog) {
		return xmlLogDao.totalComListlen(Comlog);
	}

	public Object doPagWatch() {
		return xmlLogDao.totalWatch();
	}

	/**
	 * 数据验证——模板验证分页查询
	 * 
	 * @param cardid
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<Template> queryCheckData(int start, int limit) {
		// 所有有误的模板
		List<Template> temp = xmlLogDao.queryAllProd(start, limit);
		// 产品对应所有启用模板
		List<Template> tem = xmlLogDao.queryCheckData();
		for (Template r : temp) {
			r.setType("0");
			for (Template s : tem) {
				if (r.getCardid().equals(s.getCardid())) {
					r.setType(s.getType());
					// tem.remove(s);
				}
			}
		}
		return temp;
	}

	/**
	 * 数据验证——模板验证分页总条数
	 * 
	 * @return
	 */
	public Object totalCDTemp() {
		return xmlLogDao.totalCDTemp();
	}

	/**
	 * 数据验证——规则验证分页查询
	 * 
	 * @param temp
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<CheckRuleData> queryArea(String period, int start, int limit) {
		// 所有模板区域
		List<CheckRuleData> crd = xmlLogDao.queryArea(start, limit);
		// 有默认规则的区域的List
		List<CheckRuleData> newcrd = new ArrayList<CheckRuleData>();
		for (CheckRuleData s: crd) {
			// 匹配有默认规则的区域
			if(null!=period && !"".equals(period)){
				period = period.substring(0,7);
				s.setRuleid(period);	
			}
			int yn = xmlLogDao.queryDefRule(s);
			if (yn > 0) {
				s.setRuleid(null);
				newcrd.add(s);
			}
		}
		crd.removeAll(newcrd);
		return crd;
	}

	/**
	 * 数据验证——规则验证分页总条数
	 * 
	 * @return
	 */
	public Object queryRuleTotalPage() {
		return xmlLogDao.queryRuleTotalPage();
	}

	/**
	 * V+,积分,AllReady查询
	 * 
	 * @param chek
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<Check> queryVJACheck(Check chek, int start, int limit) {
		if(null!=chek.getPeriod()&&!"".equals(chek.getPeriod())){
			String dat = chek.getPeriod().substring(0,10).replaceAll("-", "");
			chek.setPeriod(dat);			
		}
		return xmlLogDao.queryVJACheck(chek, start, limit);
	}
	
	/**
	 * V+,积分,AllReady查询总条数
	 * 
	 * @param temp
	 * @return
	 */
	public int queryCheckPage(Check chek){
		return xmlLogDao.queryCheckPage(chek);
	}
	
	/**
	 * 全部通过
	 * 
	 * @return
	 */
	public int updateCheck(String period){
		return xmlLogDao.updateCheck(period);
	}
	
	/**
	 * 查询账单城市设置为空项
	 * 
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<UserBase> queryCity(String period,int start,int limit){
		return xmlLogDao.queryCity(period,start,limit);
	}
	
	/**
	 * 分页总条数
	 * 
	 * @return
	 */
	public int queryCityPage(String period){
		return xmlLogDao.queryCityPage(period);
	}
	/**
	 * 设置账单城市为其他城市
	 * 
	 * @return
	 */
	public int updateCity(String period){
		return xmlLogDao.updateCity(period);
	}
	
	/**
	 * 积分验证,分页查询
	 * 
	 * @param period 账单日
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<PointInfo> queryJFCheck(String period,int start,int limit){
		return xmlLogDao.queryJFCheck(period, start, limit);
	}
	
	/**
	 * 积分验证分页总条数
	 * 
	 * @param period
	 * @return
	 */
	public int queryJFCheckPage(String period){
		return xmlLogDao.queryJFCheckPage(period);
	}
	
	/**
	 * 将验证有误的积分插入到check_point表中
	 * 
	 * @param period
	 * @return
	 */
	public int addJFCheck(String period){
		return xmlLogDao.addJFCheck(period);
	}
	
	/**
	 * 删除原表有误积分
	 * 
	 * @param period
	 * @return
	 */
	public int delJFCheck(String period){
		return xmlLogDao.delJFCheck(period);		
	}
	
	/**
	 * 获取默认账期
	 * 
	 * @return
	 */
	public String getPeriod(){
		Map<String,String> map=new HashMap<String,String>();
		map = makeXmlDao.getConfig();
		return map.get("PERIOD");
	}
	
	/**
	 * 预印纸验证
	 * 
	 * @param period
	 * @return
	 */
	public List<Check> queryYYZ(String period){
		return xmlLogDao.queryYYZ(period);
	}
}
